package com.example.zhangwei.ui.viewpagerfragment;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.example.zhangwei.R;
import com.example.zhangwei.ui.viewpagerfragment.fragment.OneFragment;
import com.example.zhangwei.ui.viewpagerfragment.fragment.ThreeFragment;
import com.example.zhangwei.ui.viewpagerfragment.fragment.TwoFragment;
import com.example.zhangwei.util.MyLogger;



public class ViewPagerFragment extends FragmentActivity implements OnPageChangeListener{
	
	private MyLogger log = MyLogger.getLogger("ViewPagerFragment");
	
	private ViewPager viewPager;
	
	private int[] menus;
	
	public DetailFragmentAdapter fmAdapter;
	
	public ArrayList<Fragment> mFragmentGroup;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewpager_fragmengt_layout);
			
		initView();
	}
	
	private void initView()
	{
		viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setOffscreenPageLimit(4);
        menus = new int[] {R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4};
        
        int i = 0;
        for (int id : menus)
        {
            RadioButton rb = (RadioButton) findViewById(id);
            rb.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    viewPager.setCurrentItem((Integer) v.getTag());
                }
            });
            rb.setTag(i++);
        }
        //实例化每个fragment
        mFragmentGroup = new ArrayList<Fragment>();
        mFragmentGroup.add(new OneFragment(ViewPagerFragment.this));
        mFragmentGroup.add(new TwoFragment(ViewPagerFragment.this));
        mFragmentGroup.add(new ThreeFragment());
        mFragmentGroup.add(new ThreeFragment());
        
        viewPager.setOnPageChangeListener(this);
        fmAdapter = new DetailFragmentAdapter(this.getSupportFragmentManager(), mFragmentGroup);
        viewPager.setAdapter(fmAdapter);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		((RadioButton) findViewById(menus[arg0])).setChecked(true);
	}
	
	public class DetailFragmentAdapter extends FragmentPagerAdapter
	{
		private FragmentManager fm;
		
		private ArrayList<Fragment> mFragmentGroup;
		
		public DetailFragmentAdapter(FragmentManager fm,ArrayList<Fragment> fragmentGroup)
		{
			super(fm);
			this.fm = fm;
			mFragmentGroup = fragmentGroup;
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) 
		{
			// TODO Auto-generated method stub
			return mFragmentGroup.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mFragmentGroup.size();
		}
		
		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return POSITION_NONE;
		}
		
		//解决viewpager替换指定fragmengt
		public void setFragments(Fragment frag,int id) {
			FragmentTransaction ft = fm.beginTransaction();
			ft.remove(this.mFragmentGroup.get(id));
			ft.commit();
			ft=null;
			fm.executePendingTransactions();
			this.mFragmentGroup.set(id, frag);
			notifyDataSetChanged();
		}
	}
	
	public void setRefreshFragmentGroup()
	{
		fmAdapter.notifyDataSetChanged();
		viewPager.invalidate();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		log.e("----------onKeyDown-----------");
		return super.onKeyDown(keyCode, event);	
	}
	
	@SuppressLint("NewApi")
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		log.e("----------onBackPressed-----------");
	}

		
}
